package cz.muni.fi.pa036.kiss.serviceimpl;

import cz.muni.fi.pa036.kiss.Entity.KolejAssembler;
import cz.muni.fi.pa036.kiss.Entity.KolejDTO;
import cz.muni.fi.pa036.kiss.Entity.StudentAssembler;
import cz.muni.fi.pa036.kiss.Entity.StudentDTO;
import cz.muni.fi.pa036.kiss.Entity.Ubytovani;
import cz.muni.fi.pa036.kiss.Entity.UbytovaniAssembler;
import cz.muni.fi.pa036.kiss.Entity.UbytovaniDTO;
import cz.muni.fi.pa036.kiss.dao.UbytovaniDAO;
import cz.muni.fi.pa036.kiss.service.UbytovaniService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class UbytovaniServiceImpl implements UbytovaniService {

    @Autowired
    private UbytovaniDAO ubytDao;
    @Autowired
    private UbytovaniAssembler assembler;
    @Autowired
    private StudentAssembler studentAss;
    @Autowired
    KolejAssembler kolejAss;

    @Transactional
    @Override
    public void create(UbytovaniDTO ubytovani) {
        if (ubytovani == null) {
            throw new IllegalArgumentException("ubytovani is null: ");
        }
        if (ubytovani.getId() != null) {
            throw new IllegalArgumentException("id is not null");
        }
        Ubytovani ubyt = assembler.createEntityfromDTO(ubytovani);
        ubytDao.create(ubyt);
        ubytovani.setId(ubyt.getId());
    }

    @Transactional(readOnly = true)
    @Override
    public UbytovaniDTO get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
        UbytovaniDTO ubytovani = assembler.createDTOfromEntity(ubytDao.get(id));
        if (ubytovani == null) {
            return null;
        } else {
            return ubytovani;
        }
    }

    @Transactional
    @Override
    public void update(UbytovaniDTO ubytovani) {
        if (ubytovani == null) {
            throw new IllegalArgumentException("ubytovani is null");
        }
        ubytDao.update(assembler.createEntityfromDTO(ubytovani));
    }

    @Transactional
    @Override
    public void remove(UbytovaniDTO ubytovani) {
        if (ubytovani == null) {
            throw new IllegalArgumentException("ubytovani is null");
        }
        ubytDao.update(assembler.createEntityfromDTO(ubytovani));
    }

    @Transactional(readOnly = true)
    @Override
    public List<UbytovaniDTO> findAll() {
        List<Ubytovani> all = ubytDao.findAll();
        List<UbytovaniDTO> allDto = new ArrayList<UbytovaniDTO>();
        for (Ubytovani ubytovani : all) {
            allDto.add(assembler.createDTOfromEntity(ubytovani));
        }
        return allDto;

    }

    @Transactional(readOnly = true)
    @Override
    public List<UbytovaniDTO> findByKolej(KolejDTO kolej) {
        if (kolej == null) {
            throw new IllegalArgumentException("kolej is null");
        }
        List<Ubytovani> all = ubytDao.findByKolej(kolejAss.createEntityfromDTO(kolej));
        List<UbytovaniDTO> allDto = new ArrayList<UbytovaniDTO>();
        for (Ubytovani ubytovani : all) {
            allDto.add(assembler.createDTOfromEntity(ubytovani));
        }
        return allDto;
    }

    @Override
    public List<UbytovaniDTO> findByStudent(StudentDTO student) {
        if (student == null) {
            throw new IllegalArgumentException("student is null");
        }
        List<Ubytovani> all = ubytDao.findByStudent(studentAss.createEntityfromDTO(student));
        List<UbytovaniDTO> allDto = new ArrayList<UbytovaniDTO>();
        for (Ubytovani ubytovani : all) {
            allDto.add(assembler.createDTOfromEntity(ubytovani));
        }
        return allDto;
    }
}
